package sqlInject;

import beans.ResponseInfo;
import burp.*;
import result.AttackResult;
import sqlInject.api.SqlInjectCheck;
import threadManger.ThreadManager;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * Created by shuaidong on 2017/6/25.
 */
public class SqlInjectCheckImpl implements SqlInjectCheck{
    public static   ExecutorService executorService = ThreadManager.createExcutorService();
    private IBurpExtenderCallbacks callbacks;
    private List<AttackResult> attackResultList;

    private PrintWriter stdout;

    private ScannerInsertPointProvider pointProvider;

    public SqlInjectCheckImpl(IBurpExtenderCallbacks callbacks,PrintWriter stdout,ScannerInsertPointProvider pointProvider) {
        this.callbacks = callbacks;
        this.stdout = stdout;
        this.pointProvider = pointProvider;
    }


    public IScanIssue doGetCheck(IHttpRequestResponse baseRequestResponse) {
        ResponseInfo responseInfo = new ResponseInfo(this.callbacks.getHelpers());
        responseInfo = responseInfo.getResponseInfo(baseRequestResponse);
      //  this.stdout.println("dogetcheck:"+this.callbacks.getHelpers().bytesToString(baseRequestResponse.getRequest()));

        List<IScannerInsertionPoint> insertionPoints = pointProvider.getInsertionPoints(baseRequestResponse);


        if (!insertionPoints.isEmpty()){

            SqlInjectType sqlInjectType = new SqlInjectType(SqlInjectType.InjectType.ORDINARYINJECTION,baseRequestResponse,callbacks,insertionPoints);

            executorService.submit(sqlInjectType);



            //do timeBindInject



            //do errorInject
            SqlInjectType errorInject = new SqlInjectType(SqlInjectType.InjectType.ERRORINJECT,baseRequestResponse,callbacks,insertionPoints);
            executorService.submit(errorInject);




        }

        return null;
    }

    public IScanIssue doPostCheck(IHttpRequestResponse baseRequestResponse) {
        ResponseInfo responseInfo = new ResponseInfo(this.callbacks.getHelpers());
        responseInfo = responseInfo.getResponseInfo(baseRequestResponse);

        List<IScannerInsertionPoint> ordernaryinsertionPoints = pointProvider.getInsertionPoints(baseRequestResponse);
        /*List<IScannerInsertionPoint> errorInjectinsertionPoints = new ArrayList<IScannerInsertionPoint>();
        errorInjectinsertionPoints.addAll(ordernaryinsertionPoints);*/
        if (!ordernaryinsertionPoints.isEmpty()) {
            //do ordertySqlinject
            //new Thread((Runnable) new SqlInjectType(SqlInjectType.InjectType.ORDINARYINJECTION,baseRequestResponse,callbacks)).start();

            //  this.stdout.println(resposeString);

            SqlInjectType sqlInjectType = new SqlInjectType(SqlInjectType.InjectType.ORDINARYINJECTION, baseRequestResponse, callbacks, ordernaryinsertionPoints);

            executorService.submit(sqlInjectType);

            SqlInjectType errorInject = new SqlInjectType(SqlInjectType.InjectType.ERRORINJECT,baseRequestResponse,callbacks,ordernaryinsertionPoints);
            executorService.submit(errorInject);
        }

        return null;
    }



}
